-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(chapter_hashing): Add js and ts codes for chapter hashing #675
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎ 1 Ignored Deployment
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yuan0221 Thank you for the clean code, the TS code may follow the JS to address.
const index = this.#hashFunc(key); | ||
const bucket = this.#buckets[index]; | ||
// 遍历桶,若找到 key 则返回对应 val | ||
for (let pair of bucket) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for (let pair of bucket) { | |
for (const pair of bucket) { |
const index = this.#hashFunc(key); | ||
const bucket = this.#buckets[index]; | ||
// 遍历桶,若遇到指定 key ,则更新对应 val 并返回 | ||
for (let pair of bucket) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for (let pair of bucket) { | |
for (const pair of bucket) { |
for (let pair of bucket) { | ||
if (pair.key === key) { | ||
this.#buckets[index] = bucket.filter( | ||
(pair) => pair.key !== key | ||
); | ||
this.#size--; | ||
break; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The filter method goes through every pair in the bucket twice (once for the for...of loop and once for the filter), splice will be better
for (let bucket of bucketsTmp) { | ||
for (let pair of bucket) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for (let bucket of bucketsTmp) { | |
for (let pair of bucket) { | |
for (const bucket of bucketsTmp) { | |
for (const pair of bucket) { |
|
||
/* 打印哈希表 */ | ||
print() { | ||
for (let bucket of this.#buckets) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for (let bucket of this.#buckets) { | |
for (const bucket of this.#buckets) { |
function addHash(key) { | ||
let hash = 0; | ||
const MODULUS = 1000000007; | ||
for (let c of key) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for (let c of key) { | |
for (const c of key) { |
function mulHash(key) { | ||
let hash = 0; | ||
const MODULUS = 1000000007; | ||
for (let c of key) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for (let c of key) { | |
for (const c of key) { |
function xorHash(key) { | ||
let hash = 0; | ||
const MODULUS = 1000000007; | ||
for (let c of key) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for (let c of key) { | |
for (const c of key) { |
function rotHash(key) { | ||
let hash = 0; | ||
const MODULUS = 1000000007; | ||
for (let c of key) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for (let c of key) { | |
for (const c of key) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you @yuan0221, there are have 4 positions that need to be updated.
this.#buckets = new Array(this.#capacity).fill(null); | ||
this.#size = 0; | ||
// 将键值对从原哈希表搬运至新哈希表 | ||
for (let pair of bucketsTmp) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need to be updated
@justin-tse 4 positions have been updated. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yuan0221 Thank you!
|
||
/* 打印哈希表 */ | ||
print() { | ||
for (let pair of this.#buckets) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need to be updated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @krahets, I don't have another new review advice. Wait for you to check and merge.
hi @justin-tse i fixed the bug, and added the js and ts codes for chapter 7.3. |
codes/javascript/modules/TreeNode.js
Outdated
if (arr[i] !== null) node.right = new TreeNode(arr[i]); | ||
queue.push(node.left, node.right); | ||
} else { | ||
i += 2; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please see #678 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @yuan0221 . I recommend creating a new PR for the array_binary_tree
implementation.
Please remove it and I will merge this one. Thx!
a536413
to
675841d
Compare
ok, I have removed the array_binary_tree commit, please check and merge |
If this PR is related to coding or code translation, please fill out the checklist and paste the console outputs to the PR.